<template>
    <div>
        <div class="search-div" style="margin-bottom: 10px;">
            <el-form label-width="70px" size="small">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="关键字">
                            <el-input style="width: 95%" v-model="searchObj.bName" placeholder="楼宇名"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="关键字">
                            <el-input style="width: 95%" v-model="searchObj.dName" placeholder="宿舍名"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="操作时间">
                            <el-date-picker v-model="createTimes" type="datetimerange" range-separator="至"
                                start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"
                                style="margin-right: 10px;width: 100%;" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="display:flex">
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
                </el-row>
            </el-form>
        </div>

        <!-- 工具条 -->
        <el-dialog title="新增学生" :visible.sync="dialogVisible" width="40%">
            <el-form ref="dataForm" :model="CheckInList" label-width="100px" size="small" style="padding-right: 40px;">
                <el-form-item label="新增学生" prop="sName">
                    <el-select v-model="CheckInList.sid" placeholder="请选择学生">
                        <el-option v-for="item in studentList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="CloseDialog" size="small" icon="el-icon-refresh-right">取 消</el-button>
                <el-button type="primary" icon="el-icon-check" @click="save" size="small">确 定</el-button>
            </span>
        </el-dialog>


        <el-dialog title="删除学生" :visible.sync="dialogVisible2" width="40%">
            <!-- <el-form-item label="楼宇名">
                <el-input disabled :value="CheckInList.bname"></el-input>
            </el-form-item>
            <el-form-item label="宿舍名">
                <el-input disabled :value="CheckInList.dname"></el-input>
            </el-form-item> -->

            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">
                全选
            </el-checkbox>
            <div style="margin: 15px 0;"></div>
            <el-checkbox-group v-model="studentIds" @change="handleCheckedChange">
                <el-checkbox v-for="student in studentList2" :label="student.id" :key="student.id">
                    {{ student.name }}
                </el-checkbox>
            </el-checkbox-group>

            <span slot="footer" class="dialog-footer">
                <el-button @click="CloseDialog" size="small" icon="el-icon-refresh-right">取 消</el-button>
                <el-button type="primary" icon="el-icon-check" @click="remove()" size="small">确 定</el-button>
            </span>
        </el-dialog>

        <div class="app-container">
            <!-- 列表 -->
            <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;">

                <el-table-column label="序号" width="70" align="center">
                    <template slot-scope="scope">
                        {{ (page - 1) * limit + scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="bname" label="楼宇名" width="150" />
                <el-table-column prop="dname" label="宿舍名" width="100" />
                <el-table-column prop="gender" label="宿舍类别" width="100" >
                    <template scope="scope">
                        {{ scope.row.gender === 0 ? '男生宿舍' : '女生宿舍' }}
                    </template>
                </el-table-column>
                <el-table-column prop="sname" label="入住学生" width="180" />
                <el-table-column prop="max" label="最大入住数量" width="180" />
                <el-table-column prop="number" label="已入住人数" width="180" />
                <el-table-column prop="createTime" label="创建时间" />
                <!-- <el-table-column prop="updateTime" label="修改时间" /> -->
                <el-table-column label="操作" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="success" icon="el-icon-plus"  :disabled="scope.row.number === scope.row.max"  size="mini" @click="edit1(scope.row)" title="新增" />
                        <el-button type="danger" icon="el-icon-delete" :disabled="scope.row.number === 0"  size="mini" @click="edit2(scope.row)" title="删除" />
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-pagination :current-page="page" :total="total" :page-size="limit"
                style="padding: 30px 0; text-align: center;" layout="total, prev, pager, next, jumper"
                @current-change="fetchData" />

        </div>
    </div>
</template>
<script>

import api from '@/api/checkIn/checkIn.js'
import sapi from '@/api/student/student.js'


const defaultForm = {
    id: '',
    dormitoryId: '',
    sId: '',
    max: '',
    number: '',
    name: '',
    activated: 1,
    bName: '',
    gender:0,
    dName: '',
    sName: ''
}
export default {
    data() {
        return {
            listLoading: true, // 数据是否正在加载
            list: null, // banner列表
            total: 0, // 数据库中的总记录数
            page: 1, // 默认页码
            limit: 5, // 每页记录数
            searchObj: {}, // 查询表单对象
            CheckInList: defaultForm,
            createTimes: [],
            studentList: [],
            studentList2: [],
            dialogVisible2: false,
            dialogVisible: false,
            saveBtnDisabled: false,

            dialogRoleVisible: false,
            // allRoles: [], // 所有角色列表
            // userRoleIds: [], // 用户的角色ID的列表
            studentIds: [], // 宿舍的学生ID的列表
            isIndeterminate: false, // 是否是不确定的

            checkAll: false // 是否全选



        }
    },
    created() {
        this.fetchData();
        // this.getStudent();
    },
    methods: {

        fetchData(page = 1) {
            this.page = page;
            if (this.createTimes && this.createTimes.length == 2) {
                this.searchObj.createTimeBegin = this.createTimes[0];
                this.searchObj.createTimeEnd = this.createTimes[1];
            }
            api.getPageInfo(this.page, this.limit, this.searchObj).then(
                response => {
                    //this.list = response.data.list

                    this.list = response.data.records;
                    this.total = response.data.total;
                    // console.log("列表：", this.list)
                    // 数据加载并绑定成功
                    this.listLoading = false;
                }
            )
        },
        getStudent() {
            // 根据楼宇的类别获取所有相应的未设置宿舍的学生
            sapi.getStudent(this.CheckInList.gender).then((response) => {
                this.studentList = response.data
                // console.log("获取所有未设置宿舍的学生: ", this.studentList)

            })
        },

        edit1(row) {
            this.CheckInList = row
            this.CheckInList.id = ""
			//新增
            this.getStudent()
            //c.打开弹框
            this.dialogVisible = true;
        },
        edit2(row) {
            // console.log("删除学生")
            //c.打开弹框
            this.dialogVisible2 = true;
            this.CheckInList = row;
            this.CheckInList.id = "";
            // 获取该宿舍的所有学生
            api.findById(this.CheckInList.dormitoryId).then((response) => {

                this.studentList2 = response.data.studentList2;
                // this.studentIds = response.data.studentIds;
                // this.checkAll = this.studentIds.length === this.studentList2.length
                // this.isIndeterminate = this.studentIds.length > 0 && this.studentIds.length < this.studentList2.length;
                // console.log("该宿舍学生信息：", this.studentList2)
                // console.log("该宿舍学生id：", this.studentIds)

            })

        },

        resetData() {
            // 1.清空条件搜索
            this.searchObj = {};
            //2.清空日期条件
            this.createTimes = [];
            //3.刷新数据列表
            this.fetchData();
        },



        save() {
            api.save(this.CheckInList)
                .then((response) => {
                    // 提示
                    this.$message({
                        type: "success",
                        message: "保存成功!",
                    });
                    this.CheckInList = {};
                    this.dialogVisible = false;
                    // 刷新页面
                    this.fetchData(this.page);
                     // 刷新未设置宿舍的学生列表
                    // this.getStudent();

                });
        },
        CloseDialog() {
            this.CheckInList = {};
            this.dialogVisible = false
            this.dialogVisible2 = false
        },

        handleCheckAllChange(value) {// value 当前勾选状态true/false
            // 如果当前全选, userRoleIds就是所有角色id的数组, 否则是空数组
            this.studentIds = value ? this.studentList2.map(item => item.id) : []
            // 如果当前不是全选也不全不选时, 指定为false
            this.isIndeterminate = false
        },
        /*
            角色列表选中项发生改变的监听
            */
        handleCheckedChange(value) {
            const { studentIds, studentList2 } = this
            this.checkAll = studentIds.length === studentList2.length && studentList2.length > 0
            this.isIndeterminate = studentIds.length > 0 && studentIds.length < studentList2.length
        },
        // 根据id删除数据
        remove() {

            api.removeById(this.CheckInList.dormitoryId, this.studentIds)
                .then((response) => {
                    // 提示
                    this.$message({
                        type: "success",
                        message: "删除成功!",
                    });
                    this.dialogVisible2 = false;
                    // 刷新页面
                    this.fetchData(this.page);
                    this.getStudent();
                });

        },


    }
}
</script>